package com.dingup.mock.util;

import com.dingup.mock.bean.exam.TsMockReport;
import com.dingup.mock.bean.exam.TsMockReportData;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by liuhaoyu on 16/7/15.
 */
public class MockUtil {
    private static Map<String, Integer> map = new HashMap<>();
    private static int[] speakingScoreArr = {1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15, 17, 18, 19, 20, 22, 23, 24, 26, 27, 28, 29, 30};
    private static int[] writingScoreArr = {0, 4, 4, 8, 8, 10, 11, 12, 14, 15, 17, 18, 20, 21, 22, 24, 25, 27, 28, 29, 30};
    private static double readingTotal = 45.0; //总分45,学生得分*45/套题总分(42,43,44)=实际所得总分,四舍五入

    static {
        map.put("TPO2", 42);
        map.put("TPO6", 44);
        map.put("TPO7", 43);
        map.put("TPO8", 44);
        map.put("TPO9", 44);
        map.put("TPO14", 44);
        map.put("TPO15", 44);
        map.put("TPO32", 44);
        map.put("20150516_real_test", 44);
    }

    //阅读特殊题型计算
    public static Integer matchesScore(String questionTitle) {
        String pattern = "(This question is worth \\s*)(\\d)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(questionTitle);
        if (m.find()) {
            return Integer.parseInt(m.group(2));
        } else {
            return 1;
        }
    }

    //换算阅读分数
    public static Integer getReadingScore(String subjectName, int index) {
        subjectName = subjectName.toUpperCase();
        Integer sum = map.get(subjectName);
        if (sum != null) {
            return Math.round(Math.round(index * readingTotal / sum));
        }
        return index;
    }

    // 口语24分换算成30分
    public static Integer getSpeakingScore(int score) {
        return speakingScoreArr[score];
    }

    // 写作10分换算30分
    public static Integer getWritingScore(float score) {
        int index = (int) (score * 2);
        return writingScoreArr[index];
    }

    //封装模考报告统计数据
    public static TsMockReportData createMockReportData(TsMockReport mr) throws Exception {
        String readingScore = mr.getReadingScore();
        String listeningScore = mr.getListeningScore();
        String speakingScores = mr.getSpeakingScore();
        String writingScores = mr.getWritingScore();
        TsMockReportData reportData = new TsMockReportData();
        reportData.setExamId(mr.getExamId());
        reportData.setUserName(mr.getUserName());//考生姓名
        if (StringUtils.isEmpty(readingScore) || StringUtils.isEmpty(listeningScore)) {
            throw new Exception("阅读或听力分数为空");
        }
        String[] readingArr = readingScore.split(",");
        String[] listeningArr = listeningScore.split(",");
        if (readingArr.length < 4 || listeningArr.length < 4) {
            throw new Exception("阅读或听力分数有误");
        }
        //阅读
        reportData.setReadingCorrectNum(Integer.parseInt(readingArr[0]));
        reportData.setReadingWrongNum(Integer.parseInt(readingArr[1]));
        reportData.setReadingUndoNum(Integer.parseInt(readingArr[2]));
        reportData.setReadingScore(Integer.parseInt(readingArr[3]));
        reportData.setReadingSumNum(Integer.parseInt(readingArr[0]) + Integer.parseInt(readingArr[1]) + Integer.parseInt(readingArr[2]));
        //听力
        reportData.setListeningCorrectNum(Integer.parseInt(listeningArr[0]));
        reportData.setListeningWrongNum(Integer.parseInt(listeningArr[1]));
        reportData.setListeningUndoNum(Integer.parseInt(listeningArr[2]));
        reportData.setListeningSumNum(Integer.parseInt(listeningArr[0]) + Integer.parseInt(listeningArr[1]) + Integer.parseInt(listeningArr[2]));
        reportData.setListeningScore(Integer.parseInt(listeningArr[3]));
        int totalScore = reportData.getReadingScore() + reportData.getListeningScore();
        //口语
        if (!StringUtils.isEmpty(speakingScores)) {
            String[] speakingArr = speakingScores.trim().split(",");
            if (speakingArr.length < 6) {
                throw new Exception("口语分数格式有误");
            }
            reportData.setSpeakingScoreArr(speakingArr);
            int speakingScore = 0;
            for (int i = 0; i < speakingArr.length; i++) {
                speakingScore += Integer.parseInt(speakingArr[i]);
            }
            reportData.setSpeakingScore(MockUtil.getSpeakingScore(speakingScore));
            totalScore += reportData.getSpeakingScore();
        } else {
            reportData.setSpeakingScore(-1);
        }
        //写作
        if (!StringUtils.isEmpty(writingScores)) {
            String[] writingScoreArr = writingScores.trim().split(",");
            if (writingScoreArr.length < 2) {
                throw new Exception("写作分数格式有误");
            }
            reportData.setWritingScoreArr(writingScoreArr);
            int writingScore = MockUtil.getWritingScore(Float.parseFloat(writingScoreArr[0]) + Float.parseFloat(writingScoreArr[1]));
            reportData.setWritingScore(writingScore);
            totalScore += writingScore;
        } else {
            reportData.setWritingScore(-1);
        }
        //总分
        reportData.setTotalScore(totalScore);
        reportData.setExamTime(mr.getCreateTime());//考试结束时间
        return reportData;
    }
}
